package threadbase;

/**
 * java 线程组样例
 *
 * @author xujian
 * Creation date 2024/4/9 22:25
 * @version 1.0
 */
public class JavaThreadGroupDemo {

    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("myThreadGroup");

        Thread thread11 = new Thread(threadGroup,()->{
            try {
                while (true){
                    System.out.println(Thread.currentThread().getName()+"打印------");
                    Thread.sleep(500);
                }
            }catch (Exception ex){
                System.out.println(Thread.currentThread().getName()+"中断------");
            }
        },"thread1");
        thread11.start();

        ThreadGroup sonThreadGroup = new ThreadGroup(threadGroup,"myThreadGroup");


        Thread thread2 = new Thread(sonThreadGroup,()->{
            try {
                while (true){
                    System.out.println(Thread.currentThread().getName()+"打印------");
                    Thread.sleep(500);
                }
            }catch (Exception ex){
                System.out.println(Thread.currentThread().getName()+"中断------");
            }
        },"thread2");
        thread2.start();

        int i = threadGroup.activeCount();
        System.out.println("threadGroup active thread count==="+i);

        sonThreadGroup.interrupt();



    }
}
